import {Component, OnInit} from '@angular/core';
import {LoginService} from '../../services/login.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ActivatedRoute} from '@angular/router';
import {User} from '../../entity/admin';
import {removeToken} from '../../util/token.util';
import {Menu} from '../../entity/Menu';
import {map} from 'rxjs/operators';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {

  isCollapsed = false;
  isVisible = false;

  user: User;
  menus: Menu[];
  text = 'NAN';
  color = '#00a2ae';

  constructor(private route: ActivatedRoute, private message: NzMessageService, private loginService: LoginService) {
  }

  logout() {
    this.loginService.logout().subscribe(next => {
      this.message.success('退出成功！');
    }, error => {
      // this.message.error(error.message);
    }, () => {
      this.message.success('ok');
    });
    removeToken();
    this.loginService.routerRedirect();
  }

  showModal(): void {
    this.isVisible = true;
  }

  handleOk(): void {
    console.log('Button ok clicked!');
    this.isVisible = false;
  }

  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }

  ngOnInit(): void {
    this.route.data.pipe(map(res => res.homeDatas)).subscribe(([user, menu]) => {
      this.user = user;
      this.menus = menu;
      this.text = this.user.username;
    });
  }
}
